// License: GPL. For details, see LICENSE file.
package de.unistuttgart.iev.osm.bboxchooser;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

/**
 * A dialog that allows the user to choose a bounding box on the OpenStreetMap
 * Slippy Map.
 * 
 * <p>
 * The dialog is modal by default and has a minimum size of 600x500.
 * </p>
 * 
 * <p>
 * This class is based on code from JOSM.
 * </p>
 * 
 * @author Igor Podolskiy
 */
public class BBoxChooserDialog extends JDialog {

	private static final long serialVersionUID = 1L;

	private JButton okButton;
	private JButton cancelButton;
	private BBoxChooserPanel bboxChooser;

	private DialogResponse response;

	/**
	 * Creates a new BBoxChooserDialog with the default title and no owner.
	 */
	public BBoxChooserDialog() {
		setTitle(Messages.getString("BBoxChooserDialog.Title")); //$NON-NLS-1$
		setModal(true);
		setMinimumSize(new Dimension(600, 500));
		response = DialogResponse.CANCEL;
		createGUI(null);
	}

	/**
	 * Create a new BBoxChooserDialog with the specified owner and title.
	 * 
	 * @param owner
	 *            the owner window
	 * @param title
	 *            the title
	 */
	public BBoxChooserDialog(Window owner, String title, File cacheDir) {
		super(owner, title);
		setModal(true);
		setMinimumSize(new Dimension(600, 500));
		response = DialogResponse.CANCEL;
		createGUI(cacheDir);
	}

	private void createGUI(File cacheDir) {

		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentShown(ComponentEvent e) {
				response = DialogResponse.CANCEL;
			}
		});
		okButton = new JButton(Messages.getString("BBoxChooserDialog.Ok")); //$NON-NLS-1$
		cancelButton = new JButton(
				Messages.getString("BBoxChooserDialog.Cancel")); //$NON-NLS-1$
		bboxChooser = new BBoxChooserPanel(cacheDir);

		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				response = DialogResponse.OK;
				BBoxChooserDialog.this.setVisible(false);
			}
		});

		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				BBoxChooserDialog.this.setVisible(false);
			}
		});

		JPanel gridButtons = new JPanel(new GridLayout(1, 2, 10, 0));
		gridButtons.add(cancelButton);
		gridButtons.add(okButton);
		JPanel panelButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		panelButtons.add(gridButtons);

		JPanel panelDialog = new JPanel(new BorderLayout());
		panelDialog.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		panelDialog.add(bboxChooser, BorderLayout.CENTER);
		panelDialog.add(panelButtons, BorderLayout.SOUTH);

		setLayout(new BorderLayout());
		add(panelDialog, BorderLayout.CENTER);

	}

	/**
	 * Returns the selected bounding box.
	 * 
	 * @return the selected bounding box (may be <code>null</code>).
	 */
	public Bounds getBoundingBox() {
		return bboxChooser.getBoundingBox();
	}

	/**
	 * Sets the bounding box to be selected in the dialog.
	 * 
	 * @param bbox
	 *            the bounding box (may be <code>null</code>).
	 */
	public void setBoundingBox(Bounds bbox) {
		bboxChooser.setBoundingBox(bbox);
	}

	/**
	 * Returns the dialog response the user provided last time the dialog was
	 * shown.
	 * 
	 * @return the dialog response
	 */
	public DialogResponse getResponse() {
		return response;
	}
}
